    package com.drojian.demo2;

    import android.os.Bundle;
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.fragment.app.Fragment;
    import androidx.viewpager2.widget.ViewPager2;

    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    import com.drojian.demo.R;
    import com.drojian.demo2.root_fragment.BroadcastFragment;
    import com.drojian.demo2.root_fragment.CartoonFragment;
    import com.drojian.demo2.root_fragment.ResFragment;
    import com.drojian.demo3.DataFragment;
    import com.drojian.demo3.MultimediaFragment;
    import com.drojian.demo3.ServiceFragment;
    import com.drojian.demo4.InternetFragment;
    import com.drojian.root.myFragmentTitleAdapter;
    import com.google.android.material.tabs.TabLayout;
    import com.google.android.material.tabs.TabLayoutMediator;

    import java.util.ArrayList;
    import java.util.List;


    public class BaseFragment2 extends Fragment {
        private ViewPager2 viewPager;
        private TabLayout tabLayout;
        private List<Fragment> myFragmentList;
        private List<String> myTitleList;
        private myFragmentTitleAdapter myTitleAdapter;
        public static BaseFragment2 newInstance() {
            return new BaseFragment2();
        }

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                                 @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_base2, container, false);

            // 初始化ViewPager2和TabLayout
            viewPager = view.findViewById(R.id.first_viewPage);
            tabLayout = view.findViewById(R.id.top_tableLayout);
            return view;
        }

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            initData();
            myTitleAdapter = new myFragmentTitleAdapter(getChildFragmentManager(), getLifecycle(),myFragmentList, myTitleList);
            viewPager.setAdapter(myTitleAdapter);

            // 使用TabLayoutMediator关联ViewPager2和TabLayout
            new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
                tab.setText(myTitleList.get(position));
            }).attach();
        }

        private void initData() {
            myFragmentList = new ArrayList<>();
            myFragmentList.add(InternetFragment.newInstance());
            myFragmentList.add(CartoonFragment.newInstance());
            myFragmentList.add(ServiceFragment.newInstance());
            myFragmentList.add(DataFragment.newInstance());
            myFragmentList.add(MultimediaFragment.newInstance());
            myFragmentList.add(BroadcastFragment.newInstance());
            myFragmentList.add(ResFragment.newInstance());


            myTitleList = new ArrayList<>();
            myTitleList.add("网络");
            myTitleList.add("动画");
            myTitleList.add("服务");
            myTitleList.add("数据存储");
            myTitleList.add("多媒体");
            myTitleList.add("广播");
            myTitleList.add("应用资源");



        }
    }